import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/utils/utils_time.dart';
import 'package:flutter_09_game/widgets/empty/empty_no_data.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/base/widget/base_list_more_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_commission.dart';
import 'deposit_record_commission_logic.dart';

class DepositRecordCommissionPage extends StatelessWidget {
  DepositRecordCommissionPage({super.key});

  final DepositRecordCommissionLogic logic =
      Get.put(DepositRecordCommissionLogic());

  @override
  Widget build(BuildContext context) {
    return BaseListMoreGetBuilder<DepositRecordCommissionLogic>(
      emptyBuilder: () {
        return SizedBox(
          width: 1.sw,
          child: Padding(
            padding: EdgeInsets.only(top: 108.w),
            child: EmptyNoData.notDataView(),
          ),
        );
      },
      bodyTopViewBuilder: (l) {
        return Container(
          padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
          decoration: BoxDecoration(
            color: AppColor.navBgColor,
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(30.w),
              bottomRight: Radius.circular(30.w),
            ),
          ),
          child: Obx(() {
            return Row(
              children: [
                Expanded(
                    child: _buildTopDropdown([
                  LocaleKeys.allCoins.tr,
                  ...AppUserManager().walletList.map((f) => f.symbol ?? '')
                ],
                        value: logic.symbolRx.value.isEmpty
                            ? LocaleKeys.allCoins.tr
                            : logic.symbolRx.value, onChanged: (value) {
                  if (value == LocaleKeys.allCoins.tr) {
                    logic.symbolRx.value = '';
                  } else {
                    logic.symbolRx.value = value;
                  }
                })),
                8.horizontalSpace,
                Expanded(
                    child: _buildTopDropdown([
                  LocaleKeys.today.tr,
                  LocaleKeys.yesterday.tr,
                  LocaleKeys.threeDays.tr,
                  LocaleKeys.sevenDays.tr,
                  LocaleKeys.custom.tr,
                ], btnColor: AppColor.primary, value: logic.timeRx.value,
                        onChanged: (value) {
                  logic.updateTimeSelection(value);
                })),
              ],
            );
          }),
        );
      },
      builder: (_) {
        return ListView.builder(
          padding: EdgeInsets.only(top: 24.w,bottom: 24.w),
            itemCount: logic.list.length,
            itemBuilder: itemBuilder);
      },
    );
    // return Container();
  }

  /// 构建列表项
  Widget itemBuilder(BuildContext context, int index) {
    final item = logic.list[index] as ModelRecordCommission;

    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w, ).copyWith(bottom: 16.w),
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Column(
        children: [
          24.verticalSpaceFromWidth,
          // 顶部金额和币种区域
          _buildAmountSection(item),
          24.verticalSpaceFromWidth,
          // 信息行区域
          _buildInfoRows(item),
          12.verticalSpaceFromWidth,
        ],
      ),
    );
  }

  /// 构建金额和币种区域
  Widget _buildAmountSection(ModelRecordCommission item) {
    // final amount = (item.amount ?? 0) / 1000000.0; // 转换为实际金额
    // final isPositive = amount >= 0;

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // 金额显示
        Text(
          '+${item.amount.formatWallet() ??''}',
          style: TextStyle(
            fontSize: 20.sp,
            fontWeight: FontWeight.w900,
            color:  AppColor.homePrizePoolIndicator
          ),
        ),
        4.horizontalSpace,


        // 币种图标和文字
        Row(
          children: [
            AutoImage(
              UtilsOther().getAmountTagIcon(item.symbol),
              width: 20.w,
              height: 20.w,
            ),
            4.horizontalSpace,
            Text(
              item.symbol ?? '',
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建信息行区域
  Widget _buildInfoRows(ModelRecordCommission item) {
    return Column(
      children: [
        // 单号行
        _buildInfoRow(
          label: '单号',
          value: item.identity ?? '',
          showCopy: true,
        ),
        4.verticalSpaceFromWidth,
        // 游戏类型行
        _buildInfoRow(
          label: '游戏类型',
          value: item.gameName ?? '',
        ),
        4.verticalSpaceFromWidth,
        // 时间行
        _buildInfoRow(
          label: '时间',
          value: UtilsTime.depositFormatTime(item.createdAt),
        ),
      ],
    );
  }

  /// 构建信息行
  Widget _buildInfoRow({
    required String label,
    required String value,
    bool showCopy = false,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.w),
      decoration: BoxDecoration(
        color: AppColor.homeTabBackground,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Row(
        children: [
          // 标签
          Expanded(
            child: Text(
              label,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
          ),
          // 值和复制按钮
          Row(
            children: [
              Text(
                value,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w600,
                  color: AppColor.textPrimary,
                ),
              ),
              if (showCopy) ...[
                4.horizontalSpace,
                GestureDetector(
                  onTap: () => _copyToClipboard(value),
                  child: AutoImage(
                    Assets.promotionCommissionCopy,
                    width: 16.w,
                    height: 16.w,
                  ),
                ),
              ],
            ],
          ),
        ],
      ),
    );
  }



  /// 复制到剪贴板
  void _copyToClipboard(String text) {
    Clipboard.setData(ClipboardData(text: text));
    LoadingUtils.showToast('已复制到剪贴板');
  }

  Widget _buildTopDropdown(List<String> items,
      {required String value,
      required Function(String) onChanged,
      Color? btnColor}) {
    return BtnDropdown(
        items: items,
        value: value,
        // width: 1.sw - 32.w,
        // mainAxisAlignment: MainAxisAlignment.start,
        onChanged: (value) {
          onChanged.call(value);
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
                      color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
              color: Colors.black.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8.w)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w900,
                  color: btnColor ?? AppColor.textPrimary.withOpacity(0.94),
                ),
              )),
              AutoImage(
                Assets.promotionCommissionCaretDown,
                width: 16.w,
              )
            ],
          ),
        ));
  }
}
